package initialize

import (
	"fmt"
	"fresh-shop-gin/config"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// 连接mysql数据库
func InitDB(cfg *config.Config) *gorm.DB {

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		cfg.Database.User,
		cfg.Database.Password,
		cfg.Database.Host,
		cfg.Database.Port,
		cfg.Database.Name,
	)

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})

	if err != nil {
		panic("数据库连接失败")
	}

	return db
}
